package com.spiderette.ace.sdk;

import android.text.TextUtils;
import android.util.Log;

import com.spiderette.ace.sdk.ad.VideoRewardCallback;
import com.spiderette.ace.sdk.ad.SAceVideoIAP;
import com.spiderette.ace.sdk.bridge.SAceCocosBridge;
import com.spiderette.ace.sdk.ad.SAceVideoManger;
import com.spiderette.ace.sdk.ad.SAceMaxReward;
import com.spiderette.ace.sdk.utils.SAceSPUtils;

import org.json.JSONObject;

public class SAceAdSdk {

    public static void showBiddingAd(String ad_source) {
        showAd(new VideoRewardCallback() {
            @Override
            public void onAdOver() {
                adCallBack("ad_over_w",ad_source);
            }

            @Override
            public void onAdError() {
                SAceCocosBridge.getInstance().XSSdkCallback("ad_error_w", "{}");
            }
        });
    }

    private static void adCallBack(String state,String ad_source) {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("ad_source",ad_source);
            SAceCocosBridge.getInstance().XSSdkCallback(state, jsonObject.toString());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private static void showAd(VideoRewardCallback callback) {
        SAceVideoIAP service = null;
        boolean isMaxReady =  SAceMaxReward.getInstance().isReady();
        if (isMaxReady){
            service= SAceVideoManger.getReward1Instance();
            Log.e("SAce","show max reward");
        }

        if (service == null) {
            callback.onAdError();
            return;
        }
        showAd(callback, service);
    }

    public static void showAd(VideoRewardCallback callback, SAceVideoIAP service) {
        try {
            service.showVideo(SAceCocosBridge.mainGameActivity.get(), new VideoRewardCallback() {
                @Override
                public void onAdOver() {
                    if (callback != null) {
                        callback.onAdOver();
                    }
                }

                @Override
                public void onAdError() {
                    if (callback != null) {
                        callback.onAdError();
                    }
                }
            });
        } catch (Exception e) {
            try {
                callback.onAdError();
            } catch (Exception e1) {
            }
        }
    }


}
